<template>
  <div class="school-container">
    <navbar :showNav="showNav" />
    <div class="app-container">
      <div class="filter-container">
        <el-button type="primary" icon="el-icon-edit" @click="showDialog(false)"
          >添加网校</el-button
        >
      </div>
      <div>
        <el-row :gutter="20">
          <el-col :span="8" v-for="item in schoolList" :key="item.id">
            <el-card shadow="hover">
              <div
                style="display: flex; align-items: center; justify-content: space-between;"
              >
                {{ item.name }}
                <div>
                  <el-button
                    class="manage"
                    size="mini"
                    @click="showDialog(item.id)"
                    >编辑</el-button
                  >
                  <el-button
                    class="manage"
                    size="mini"
                    @click="manageSchool(item.id)"
                    style="marginLeft:0"
                    >管理</el-button
                  >
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>
    <!-- 添加网校 dialog -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="网校名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSubmit">提交</el-button>
      </span>
    </el-dialog>
    <!-- <school-dialog :dialogVisible="dialogVisible" /> -->
  </div>
</template>

<script>
import Navbar from "@/layout/components/Navbar";
import { setSchoolid } from "@/utils/auth";

// import SchoolDialog from "./components/SchoolDialog";
import { getSchoolList, addSchool, readSchool, editSchool } from "@/api/school";

export default {
  components: {
    Navbar
    // SchoolDialog
  },
  data() {
    return {
      showNav: false,
      pages: {
        total: 0,
        page: 1
      },
      schoolList: [],
      dialogVisible: false,
      ruleForm: {
        name: ""
      },
      rules: {
        name: [{ required: true, message: "请输入网校名称", trigger: "blur" }]
      },
      dialogTitle: ""
    };
  },
  methods: {
    // 获取网校列表
    async getSchoolList() {
      const {
        code,
        data: { total, items }
      } = await getSchoolList(this.pages.page);
      if (code == 20000) {
        this.pages.total = total;
        this.schoolList = items;
      }
    },
    // 添加网校 弹出 dialog
    showDialog(id) {
      if (id == false) {
        this.dialogTitle = "添加网校";
        this.dialogVisible = true;
        this.closeDialog();
      } else {
        this.dialogVisible = true;
        this.dialogTitle = "编辑网校";
        setSchoolid(id);
        this.ruleForm.id = id;
        this.readSchool(id);
      }
    },
    // 添加 编辑 网校
    addSubmit() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          if (!this.ruleForm.id) {
            const { code } = await addSchool(this.ruleForm.name);
            if (code == 20000) {
              this.dialogVisible = false;
              this.$message.success("添加成功");
              this.getSchoolList();
            }
          } else {
            const { code } = await editSchool(this.ruleForm);
            if (code == 20000) {
              this.dialogVisible = false;
              this.$message.success("更新成功");
              this.getSchoolList();
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 打开 dialog 时清空表单
    closeDialog() {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
      });
    },
    // 查看网校
    async readSchool(id) {
      const {
        code,
        data: { name }
      } = await readSchool(id);
      if (code == 20000) {
        this.ruleForm.name = name;
      }
    },
    // 管理网校
    manageSchool(id){
      setSchoolid(id)
      this.$router.push('/dashboard')
    }
  },
  created() {
    this.getSchoolList();
  }
};
</script>

<style lang="scss" scoped>
.el-col {
  margin-bottom: 10px;
  border-radius: 4px;
}
.manage {
  color: #1890ff;
  background: none;
  border: none;
}
</style>
